<?php

namespace framework\support;

use Exception;
use framework\support\Response;
use framework\support\Middleware;

class Router
{
    private static $middleware = null;

    static function run()
    {
        $uri = $_SERVER['REQUEST_URI'];
        self::$middleware = Middleware::getInstance();
        self::parse($uri);
    }

    static function parse($uri)
    {
        $controllerRoot = BasePath . '/app/controller';
        $default = "/index";
        $uriArray = explode('?', $uri);
        $path = $uriArray[0];
        if ($path == '/') {
            $path = $default;
        }
        return self::getControllerFile($path, $controllerRoot);
    }

    static function getControllerFile($path, $parentPath)
    {
        $paths = explode('/', $path);
        $route = "";
        $tempPath = $parentPath;
        $params = [];
        for ($i = 1; $i < count($paths); $i++) {
            $route .= "/" . $paths[$i];
            $className = $paths[$i];
            $tempPath = $tempPath . DIRECTORY_SEPARATOR . $paths[$i];
            $phpFile = $tempPath . '.php';
            if (file_exists($phpFile)) {
                $funName = $paths[$i + 1] ?? 'index';
                if ($funName == '') $funName = "index";
                require_once $phpFile;
                $controller = new $className();
                if (method_exists($controller,  $funName)) {
                    $params = array_slice($paths, $i + 2);
                    if (count($params) > 0) {
                        try {
                            self::$middleware->before($route);
                            $controller->$funName(...$params);
                        } catch (Exception $e) {
                            print($e->getMessage());
                            print($e->getTraceAsString());
                        }
                        return;
                    } else {
                        self::$middleware->before($route);
                        return $controller->$funName();
                    }
                } else {
                    $method = strtolower($_SERVER['REQUEST_METHOD']);
                    $funName = $paths[$i + 1] ?? 'index';
                    if ($funName == '') $funName = "index";
                    $funName .= "_" . $method;
                    if (method_exists($controller,  $funName)) {
                        $params = array_slice($paths, $i + 2);
                        if (count($params) > 0) {
                            try {
                                //执行中间件
                                self::$middleware->before($route);
                                $controller->$funName(...$params);
                            } catch (Exception $e) {
                                print($e->getMessage());
                                print($e->getTraceAsString());
                            }
                            return;
                        } else {
                            if ($method == 'delete') {
                                return Response::Error(10002, "缺失必要的参数");
                            }
                            try {
                                self::$middleware->before($route);
                                $controller->$funName();
                            } catch (Exception $e) {
                                print($e->getMessage());
                                print($e->getTraceAsString());
                            }
                            return;
                        }
                    } else {
                        return Response::Error(10001, "接口不存在");
                    }
                }
            }
        }
        return Response::Raw(404);
    }
}
